package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.dongdongshop.constant.HttpResultEnum;
import com.dongdongshop.data.DataResult;
import com.dongdongshop.pojo.goods.TbGoods;
import com.dongdongshop.pojo.goods.TbGoodsDesc;
import com.dongdongshop.pojo.item.TbItem;
import com.dongdongshop.pojo.seller.TbSeller;
import com.dongdongshop.pojo.vo.ItemCatGoodsVo;
import com.dongdongshop.service.goods.GoodsService;
import com.dongdongshop.util.FileUpload;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("goods")
public class GoodsController {

    private Logger log = LoggerFactory.getLogger(GoodsController.class);

    @Reference
    private GoodsService goodsService;

    @Autowired
    private FileUpload fileUpload;

    /**
     * 跳转商品增加页面
     */
    @RequestMapping("toGoodsEdit")
    public String toGoodsEdit(){
        return "admin/goods_edit";
    }

    /**
     * 跳转商品管理页面
     */
    @RequestMapping("toGoods")
    public String toGoods(){
        return "admin/goods";
    }

    /**
     * 增加商品
     */
    @RequestMapping(value = "addGoods",method = RequestMethod.POST)
    @ResponseBody
    public DataResult addGoods(TbGoods goods, TbGoodsDesc goodsDesc, String itemList){
        try {
            //获取登录的商家id
            TbSeller seller = (TbSeller)SecurityUtils.getSubject().getPrincipal();
            goods.setSellerId(seller.getSellerId());
            //item表
            List<TbItem> list = JSON.parseArray(itemList, TbItem.class);
            for (TbItem item : list) {
                item.setCreateTime(new Date());
                item.setSeller(seller.getNickName());
                item.setSellerId(seller.getSellerId());
            }
            goodsService.addGoods(goods,goodsDesc,list);

        }catch (Exception e){
            log.error("新增商品异常",e);
            return new DataResult(HttpResultEnum.ERROR.getCode(),HttpResultEnum.ERROR.getMessage());
        }
        return new DataResult(HttpResultEnum.SUCCESS.getCode(),HttpResultEnum.SUCCESS.getMessage());
    }


    @RequestMapping(value = "upLoadFile",method = RequestMethod.POST)
    @ResponseBody
    public DataResult upLoadFile(@RequestParam(value = "myFile")MultipartFile myFile){
        try {
            //返回上传后的url地址
            String url = fileUpload.upload(myFile);
            return new DataResult(HttpResultEnum.SUCCESS.getCode(),HttpResultEnum.SUCCESS.getMessage(),url);
        } catch (IOException e) {
            log.error("上传图片异常",e);
            return new DataResult(HttpResultEnum.ERROR.getCode(),HttpResultEnum.ERROR.getMessage());
        }
    }

    @RequestMapping("findGoodsList")
    @ResponseBody
    public DataResult findGoodsList(String auditStatus,String goodsName){

        try {
            //获取登录的用户
            TbSeller seller = (TbSeller) SecurityUtils.getSubject().getPrincipal();
            //获取seller的id传递到实现层
            List<ItemCatGoodsVo> list = goodsService.findGoodsList(auditStatus,goodsName,seller.getSellerId());
            return new DataResult(HttpResultEnum.SUCCESS.getCode(),HttpResultEnum.SUCCESS.getMessage(),list);
        }catch (Exception e){
            log.error("系统错误",e);
            return new DataResult(HttpResultEnum.ERROR.getCode(),HttpResultEnum.ERROR.getMessage());
        }
    }

    @RequestMapping("updateByStatus")
    @ResponseBody
    public DataResult updateByStatus(@RequestParam("ids[]")Long[] ids){
        try {
            goodsService.updateByStatus(ids);
            return new DataResult(HttpResultEnum.SUCCESS.getCode(),HttpResultEnum.SUCCESS.getMessage());
        }catch (Exception e){
            log.error("出现错误，查看日志{}",e);
            return new DataResult(HttpResultEnum.ERROR.getCode(),HttpResultEnum.ERROR.getMessage());
        }
    }

    @RequestMapping("deleteById")
    @ResponseBody
    public DataResult deleteById(@RequestParam("ids[]")Long[] ids){
        try {
            goodsService.deleteById(ids);
            return new DataResult(HttpResultEnum.SUCCESS.getCode(),HttpResultEnum.SUCCESS.getMessage());
        }catch (Exception e){
            log.error("出现错误，查看日志{}",e);
            return new DataResult(HttpResultEnum.ERROR.getCode(),HttpResultEnum.ERROR.getMessage());
        }
    }
}
